//
//  UIImage+Extension.swift
//  01-照片选择器
//
//  Created by 韩家奇 on 15/11/17.
//  Copyright © 2015年 hanjiaqi. All rights reserved.
//

import UIKit

extension UIImage {
    func scaleToWith(width:CGFloat) ->UIImage {
        //1.判断宽度
        if width > size.width {
            return self
        }
        //2.计算比例
        let height = size.height * width / size.width
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        //3.使用核心绘图绘制新的图像
        //1.开启上下文
        UIGraphicsBeginImageContext(rect.size)
        //2.绘图
        self.drawInRect(rect)
        //3.从上下文种取出图片
        let result  = UIGraphicsGetImageFromCurrentImageContext()
        //4.关闭上下文
        UIGraphicsEndImageContext()
        //5.返回结果
        return result
    }
}
